// Opencv2.2_test.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <opencv/cv.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <windows.h>

#include "Affine.h"
#include "Feature.h"


CvPoint loc[3];
int click_num = 0;
bool selectObject = false;
bool selectOk = false;
using namespace cv;
using namespace std;


#pragma comment(lib, "opencv_highgui220d.lib")
#pragma comment(lib, "opencv_core220d.lib")
#pragma comment(lib, "opencv_imgproc220d.lib")
#pragma comment(lib, "opencv_features2d220d.lib")
#pragma comment(lib, "winmm.lib")

void onMouse(int Event,int x,int y,int flags,void* param )
{
	printf("( %d, %d) \n",x,y);
	if(click_num < 3 && Event == CV_EVENT_LBUTTONDOWN)
	{
		selectObject = true;
		loc[click_num].x = x;
		loc[click_num].y = y;
		click_num ++;
		if(click_num == 3)
		{
			selectObject = false;
			selectOk = true;
			printf("YES\n");
		}
	}
	printf("The Event is : %d ",Event);
	printf("The flags is : %d ",flags);
//	printf("The param is : %d\n",param);
}
int main(int argc, char * argv[])
{
	MEMORYSTATUS start_memory, end_memory;	
	DWORD start, end;

	Affine myaffine = Affine();
	Feature myfeature;
	CvSize tempsize = cvSize(50,50);
	CvPoint2D32f location[3];

	start =  timeGetTime();
	GlobalMemoryStatus(&start_memory);


	CvCapture *capture;
	IplImage *frame = NULL;
	IplImage *grayframe = NULL;
	capture =cvCaptureFromCAM(0);
	cvNamedWindow("Webcam",0);
	cvSetMouseCallback("Webcam",onMouse, frame);
	while(true)
	{
		if(!selectObject)
		{
			frame = cvQueryFrame(capture);
			if(grayframe == NULL)
				grayframe = cvCreateImage(cvGetSize(frame), 8,1);
			cvCvtColor(frame, grayframe, CV_RGB2GRAY);
		}
		if(selectOk)
		{
		//	DrawAffineBox(loc, frame);
			location[0].x = loc[0].x; location[0].y = loc[0].y;
			location[1].x = loc[1].x; location[1].y = loc[1].y;
			location[2].x = loc[2].x; location[2].y = loc[2].y;
			
			
			//location[0].x = 100; location[0].y = 100;
			//location[1].x = 150; location[1].y = 150;
			//location[2].x = 150; location[2].y = 250;

			myaffine.GetAffParameters(location, tempsize);
			myaffine.PrintMatrix(myaffine.P);
			myaffine.MatrixTransform();
			myaffine.PrintMatrix(myaffine.parameters);
			myaffine.GetMatrixFromParameters();
			myaffine.PrintMatrix(myaffine.P);
			myaffine.RandomData();
			int startx = 0, starty = 400;

			Mat sampledata = Mat(tempsize.height, tempsize.width, CV_8U);
			for(int i = 0; i < SAMPLE_NUM; i ++)
			{
				myaffine.RandAffine[i].DrawBox(tempsize, frame, CV_RGB(0,0,255),1);
				myaffine.RandAffine[i].GenerateSample(sampledata, grayframe, tempsize);

				myfeature.imagedata = &sampledata;

				if(i <= 5)
				{
					uchar *ptr = (uchar *)frame->imageData;
					for(int y = 0; y < 50; y ++)
					{
						for(int x = 0; x < 50; x ++)
						{
							ptr[(starty + y) * frame->widthStep + 3 * (startx + x) ]     = GetValue_U(sampledata, y,x);
							ptr[(starty + y) * frame->widthStep + 3 * (startx + x) + 1 ] = GetValue_U(sampledata, y,x);
							ptr[(starty + y) * frame->widthStep + 3 * (startx + x) + 2 ] = GetValue_U(sampledata, y,x);

						}
					}
					myfeature.FastCorner();
					myfeature.MserCorner();
					myfeature.StarCorner();
					//myfeature.SiftDescriptor();
					myfeature.SurfDescriptor();
					//myfeature.DrawStarFeaturePoint(frame,startx, starty);
					//myfeature.DrawFastFeaturPoint(frame, startx, starty);
					//myfeature.DrawMserFeaturePoint(frame, startx,starty);
					//myfeature.DrawSiftFeaturePoint(frame, startx, starty);
					myfeature.DrawSurfFeaturePoint(frame ,startx, starty);
					startx += 50;
				}
				
			}
			
			//cvWaitKey(-1);
		}
		cvShowImage("Webcam",frame);
		if(cvWaitKey(10)>=0)
		{
			break;
		}
	}
	
	end = timeGetTime();
	GlobalMemoryStatus(&end_memory);
	printf("Cost Memory :  %d MB\n", (start_memory.dwAvailPhys - end_memory.dwAvailPhys)/(1024));
	printf("Cost Time in building tree : %d ms\n", end - start);

	
	cvReleaseCapture(&capture);
	cvDestroyWindow("Webcam");
	
	
	
	return 0;
}